import { router } from '@/router'
import { authManager } from '@/lib/auth'

const whiteList = ['/auth/callback'] // 不重定向白名单

// let views: Record<string, () => Promise<RouteComponent>>;
// views = import.meta.glob(['@/layouts/*.{vue,tsx}', '@/views/**/*.{vue,tsx}'])

/**启动前端权限*/
export default function setupPermission() {
    router.beforeEach(async (to, _from, next) => {
        if (whiteList.indexOf(to.path) !== -1 || authManager.getToken() != '') {
            next()
        }
        else {
            authManager.signinRedirect()
        }
    })
}

export function resetRouter() {
    router.getRoutes().forEach((route) => {
        const { name } = route;
        if (name && !whiteList.some((n) => n === name)) {
            router.hasRoute(name) && router.removeRoute(name);
        }
    });
}